package com.bjwl.display.controller;

import com.bjwl.display.exception.LoginException;
import com.bjwl.display.exception.SysException;
import com.bjwl.display.service.safty.LoginService;
import com.bjwl.display.util.Constants;
import com.bjwl.display.util.CurrUser;
import com.bjwl.display.util.Result;
import com.bjwl.display.vo.UserLoginVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

@RestController
@RequestMapping("/safty/login")
public class LoginController {
    @Autowired
    private LoginService loginService;
    @PostMapping("/userLogin")
    public Result login(@RequestBody UserLoginVO userLoginVO, HttpSession session){
        System.out.println("controller:"+userLoginVO);
        CurrUser currUser = null;
        try {
            currUser = loginService.login(userLoginVO);
            //将currUser放入session中 //使用常量
            session.setAttribute(Constants.SESSION_ATTR_CURRUSER,currUser);
            //
            System.out.println("登录:"+session.getId());
            return Result.success("登录成功",currUser);
        } catch (SysException e) {
            e.printStackTrace();
            return Result.fail("系统升级中");
        }catch (LoginException e){
            return Result.fail(e.getMessage());
        }

    }
    //返回未登录标记
    @GetMapping("/isLogin")
    public Result isLogin() {
        return Result.unLogin();
    }
}
